<?php
namespace app\api\controller\v1\controller\IM;

use app\api\controller\v1\model\IM\IM as IM_Connect;
use think\Controller;
use think\Db;

use app\api\controller\Api;

/**
 *IM
 */
class IM extends api
//class IM extends Controller
{
    //显示个人信息
    public function get_user_info($user_id)
    {

        $result = IM_Connect::get_User_Info($user_id);
        if (!$result) {
            return $this->returnmsg(400, '数据库没有值', $result);
        }
        return $this->returnmsg(200, '个人信息', $result);
    }

    //显示医生信息
    public function get_doctor_info($doctor_id)
    {
        $result = IM_Connect::get_Doctor_Info($doctor_id);
        unset($result[0]['rc_token']);
        if (!$result) {
            return $this->returnmsg(400, '数据库没有值', $result);
        }
        return $this->returnmsg(200, '个人信息', $result);
    }

    /**
     * 获取token接口
     * @param $user_id      用户id
     * @param string $type 类型：1-用户，2-医生,为空自动为用户
     */
    public function get_IM_Token($user_type = 1)
    {
        $id = input('get.user_id')?input('get.user_id'):input('post.user_id');
        $result = [];
        switch ($user_type) {
            case 1:
                $result = IM_Connect::get_IM_Token($id);
                break;
            case 2:
                $result = IM_Connect::get_Doctor_IM_Token($id);
                break;
            default:
                break;
        }
        if (!$result) {
            return $this->returnmsg(400, '获取token失败', $result);
        }
        return $this->returnmsg(200, '融云token获取成功', $result);

    }


    //更新融云的头像和图片
    public function refresh_IM_info($user_id)
    {
        $result = IM_Connect::refresh_rongcloud_info($user_id);
        if (!$result) {
            return $this->returnmsg(400, '更新失败', $result);
        }
        return $this->returnmsg(200, '融云头像图片刷新成功', $result);
    }

    /**
     * 　随机获取一个在线医生的,同时为医生问诊数加一
     */
    public function Get_Doctor_Status()
    {
       $doctor_info = IM_Connect::get_roll_doctor();
       if(!$doctor_info)
       {
           return $this->returnmsg(400, '没有在线医生', $doctor_info);
           //return $this->result($doctor_info,400,'没有在线医生');
       }
        return $this->returnmsg(200, '获取在线医生成功', $doctor_info);
       // return $this->result($doctor_info,200,'获取在线医生成功');
    }

    /**
     *  获取医生问诊总数目
     */
    public function get_inquisition()
    {
       $count =  IM_Connect::get_inquisition_count();
       if(!$count['status'])
       {
           return $this->returnmsg(400, $count['data']);
       }
       return $this->returnmsg(200,'获取成功' ,$count['data']);
    }

    /**
     * 添加黑名单接口
     * @param $user_id 用户id
     * @param $target_id 黑名单目标id
     * @param int $type 1用户 2医生
     */
    public function add_black_list($user_id, $target_id, $type = 1)
    {
        $add_result = IM_Connect::blacklist_add($user_id, $target_id, $type);
        if(!$add_result)
        {
            $this->result('',400,'添加失败');
        }
        return $this->result(true,200,'添加成功');
    }

    /***
     * 查询黑名单接口
     * @param $user_id 用户id
     * @param int $type 1用户 2医生
     */
    public function get_black_list($user_id, $type = 1)
    {
        $get_result = IM_Connect::blacklist_get($user_id, $type);
        if(!$get_result['status'])
        {
            $this->result('',400,'查询失败');
        }
        return $this->result($get_result['data'],200,'查询成功');
    }

    /**
     * 清除黑名单接口
     * @param $user_id
     * @param $target_id
     * @param int $type
     */
    public function delete_black_list($user_id, $target_id, $type = 1)
    {
        $add_result = IM_Connect::blacklist_delete($user_id, $target_id, $type);
        if(!$add_result)
        {
            $this->result('',400,'删除失败');
        }
        return $this->result(true,200,'删除成功');
    }



}
